package com.ruoyi.controller.jpjs.outputvalue;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.PublicService;
import com.ruoyi.service.jpjs.outputvalue.OutPutValueService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zeo
 * 产值报表
 */
@Controller
@RequestMapping("/outPutValue")
public class OutPutValueController extends BaseController {

    @Autowired
    OutPutValueService outPutValueService;

    @Autowired
    PublicService publicService;

    private String prefix = "jpjs/outputvalue";


    @GetMapping()
    public String outPutValue(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/outPutValue";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        startPage();
        return getDataTable(outPutValueService.findOutPutValueAll(map));
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(outPutValueService.add(map));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        mmap.put("obj", outPutValueService.findOutPutValueById(id));
        return prefix + "/edit";
    }

    @RequestMapping("/findOutPutValueById")
    @ResponseBody
    public BaseResult findOutPutValueById(String oid){
        Map map =  outPutValueService.findOutPutValueById(oid);
        if(map != null){
            return BaseResult.build(ResultEnum.SUCCESS,map);
        }else{
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(outPutValueService.edit(map));
    }


    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(outPutValueService.remove(ids));
    }


    //开始编写年月计划产值和实际产值


    /**
     * 跳转初始化页面
     *
     * @return
     */
    @GetMapping("/outPutValueYearMonth")
    public String outPutValueYearMonth(ModelMap mmap, String type, String id) {
        if (type != null && id != null) {
            mmap.put("type", type);
            mmap.put("id", id);
        }
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/outPutValueYearMonth";
    }


    /**
     * 获取当前用户当前权限下所有计划产值数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/findPlanValueList")
    @ResponseBody
    public TableDataInfo findPlanValueList(@RequestParam Map map) {
        Object type = map.get("type");
        Object id = map.get("id");
        if (type != null && id != null) {
            Map map1 = outPutValueService.findOutPutValueById(id.toString());
            String compId = map1.get("compId").toString();
            String proName = map1.get("proName").toString();
            map.put("compId",compId);
            if ("2".equals(type)) {
                //如果type=2代表检索工程公司和项目名称，因此将项目名称存入map中即可
                map.put("proName",proName);
            }
        }

        startPage();
        return getDataTable(outPutValueService.findPlanValueList(map));
    }

    /**
     * 跳转新增需要填写计划产值的页面
     */
    @GetMapping("/addPlanValue")
    public String addPlanValue(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/addPlanValue";
    }

    /**
     * 根据工程公司id筛选出对应的项目集合
     */
    @RequestMapping("/findProByCompId")
    @ResponseBody
    public BaseResult findProByCompId(String compId) {
        return BaseResult.build(ResultEnum.SUCCESS, outPutValueService.findProByCompId(compId));
    }

    /**
     * 新增一条年计划产值数据
     */
    @RequestMapping("/addPlanValue")
    @ResponseBody
    public AjaxResult addPlanValue(@RequestParam Map map) {
        return toAjax(outPutValueService.addPlanValue(map));
    }

    /**
     * 跳转填写某年某月的计划产值页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editPlanValue")
    public String editPlanValue(String id, ModelMap mmap) {
        mmap.put("obj", outPutValueService.findMonthOutPutValueById(id));
        return prefix + "/editPlanValue";
    }

    /**
     * 填写某年某月的计划产值
     */
    @RequestMapping("/editPlanValue")
    @ResponseBody
    public AjaxResult editPlanValue(@RequestParam Map map) {
        return toAjax(outPutValueService.editPlanValue(map));
    }

    /**
     * 判断年是否填过
     */
    @RequestMapping("/findYearPlanCount")
    @ResponseBody
    public BaseResult findYearPlanCount(@RequestParam Map map) {
        int count = outPutValueService.findYearPlanCount(map);
        if (count > 0) {
            return BaseResult.build(ResultEnum.YEAR_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    /**
     * 判断某年某月是否填过
     */
    @RequestMapping("/findYearPlanMonthCount")
    @ResponseBody
    public BaseResult findYearPlanMonthCount(@RequestParam Map map) {
        String value = outPutValueService.findYearPlanMonthCount(map);
        if (value != null) {
            return BaseResult.build(ResultEnum.YEAR_MONTH_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }


    /**
     * 获取当前用户当前权限下所有实际产值数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/findActualValueList")
    @ResponseBody
    public TableDataInfo findActualValueList(@RequestParam Map map) {
        Object type = map.get("type");
        Object id = map.get("id");
        if (type != null && id != null) {
            Map map1 = outPutValueService.findOutPutValueById(id.toString());
            String compId = map1.get("compId").toString();
            String proName = map1.get("proName").toString();
            map.put("compId",compId);
            if ("2".equals(type)) {
                //如果type=2代表检索工程公司和项目名称，因此将项目名称存入map中即可
                map.put("proName",proName);
            }
        }
        startPage();
        return getDataTable(outPutValueService.findActualValueList(map));
    }

    /**
     * 跳转新增需要填写实际产值的页面
     */
    @GetMapping("/addActualValue")
    public String addActualValue(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/addActualValue";
    }

    /**
     * 新增一条年实际产值数据
     */
    @RequestMapping("/addActualValue")
    @ResponseBody
    public AjaxResult addActualValue(@RequestParam Map map) {
        return toAjax(outPutValueService.addActualValue(map));
    }

    /**
     * 跳转填写某年某月的实际产值页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editActualValue")
    public String editActualValue(String id, ModelMap mmap) {
        mmap.put("obj", outPutValueService.findMonthOutPutValueById(id));
        return prefix + "/editActualValue";
    }

    /**
     * 填写某年某月的实际产值
     */
    @RequestMapping("/editActualValue")
    @ResponseBody
    public AjaxResult editActualValue(@RequestParam Map map) {
        return toAjax(outPutValueService.editActualValue(map));
    }

    /**
     * 判断年是否填过
     */
    @RequestMapping("/findYearActualCount")
    @ResponseBody
    public BaseResult findYearActualCount(@RequestParam Map map) {
        int count = outPutValueService.findYearActualCount(map);
        if (count > 0) {
            return BaseResult.build(ResultEnum.YEAR_ACTUAL_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    /**
     * 判断某年某月是否填过
     */
    @RequestMapping("/findYearActualMonthCount")
    @ResponseBody
    public BaseResult findYearActualMonthCount(@RequestParam Map map) {
        String value = outPutValueService.findYearActualMonthCount(map);
        if (value != null) {
            return BaseResult.build(ResultEnum.YEAR_ACTUAL_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }
}

